package com.example.fiction.ui.activity.reader.server;

import com.example.fiction.utils.AnalysisJsonRequestUtils;
import com.example.fiction.db.BooksDBHelper;
import com.example.fiction.ui.activity.reader.entry.ChapterContentBean;
import com.example.fiction.ui.activity.reader.entry.ChapterItemBean;

import org.litepal.LitePal;

import java.util.List;

/**
 * auth: 姚鸿杰
 * time: 2019/3/24 8:39
 **/
public class LocalServer {
    /**
     * 请求书籍目录
     *
     * @param bookId 书ID
     */
    public static void getChapterList(final String bookId, final OnResponseCallback onResponseCallback) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                List<ChapterItemBean> chapters;

                BooksDBHelper book = LitePal.where("bookId = ?", bookId).findFirst(BooksDBHelper.class);
                String chapterList = book.getChapterList();
                chapters = AnalysisJsonRequestUtils.getAnalysisJsonList(chapterList, ChapterItemBean[].class);
                onResponseCallback.onSuccess(chapters);
            }
        }).start();
    }

    /**
     * 模拟网络同步下载
     *
     * @return 章节内容
     */
    public static ChapterContentBean syncDownloadContent(ChapterItemBean chapterItemBean) {
        ChapterContentBean chapterContentBean = new ChapterContentBean();
        chapterContentBean.setChapterId(chapterItemBean.getChapterId());
        chapterContentBean.setChapterName(chapterItemBean.getChapterName());
        StringBuilder contentBuilder = new StringBuilder();
        while (contentBuilder.length() < 1000) {
            contentBuilder.append(String.valueOf(chapterItemBean.getChapterName() + "</p>" +
                    "　　</p>" +
                    "　　世界上一成不变的东西，只有“任何事物都是在不断变化的”这条真理。</p> —— 斯里兰卡<br><br>" +
                    "　　</p>" +
                    "　　我需要三件东西：爱情友谊和图书。然而这三者之间何其相通！炽热的爱情可以充实图书的内容，图书又是人们最忠实的朋友。</p> —— 蒙田<br><br>" +
                    "　　</p>" +
                    "　　生活有度，人生添寿。</p> —— 书摘<br><br>"));
//            contentBuilder.append(LocalConstant.CONTENT);

        }
        chapterContentBean.setChapterContent(contentBuilder.toString());
        return chapterContentBean;
    }

    public interface OnResponseCallback {
        void onSuccess(List<ChapterItemBean> chapters);

        void onError(Exception e);
    }
}
